package kr.oss.cho.util;

import java.io.Reader;

import kr.oss.cho.mapper.SqlVeiwMapper;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SqlSessionFactoryUtil {

	public static SqlSessionFactory sqlMapper = null;
	
	public static SqlSessionFactory getInstance() {

		if (sqlMapper == null) {

			try {

				String resource = "resource/config.xml";

				Reader reader = Resources.getResourceAsReader(resource);

				sqlMapper = new SqlSessionFactoryBuilder().build(reader);

				reader.close();

			} catch (Exception e) {

				e.printStackTrace();

			}
			
			
			SqlSession session = sqlMapper.openSession();
			
			try{
				SqlVeiwMapper mapper = session.getMapper(SqlVeiwMapper.class);
				
				int exist = 0;
				exist = mapper.existTableTnSqlinjection();
				
				if(exist < 1){
					 mapper.createTableTnSqlinjection();
				}
				
			}catch(Exception e){
				System.out.println(e.toString());
			}finally{
				session.close();
			}

		}

		return sqlMapper;

	}
}
